package com.jie.component.validation.validator.impl;

import com.jie.component.validation.annotion.NotEmpty;
import com.jie.component.validation.validator.ConstraintValidator;

import java.util.Collection;
import java.util.Map;

/**
 * 非空的参数校验器
 */
public class NotEmptyValidator implements ConstraintValidator<NotEmpty> {
    
    @Override
    public boolean isValid(Object value, NotEmpty annotation) {
        if (value == null) {
            return false;
        }
        if (value instanceof String) {
            return !((String) value).trim().isEmpty();
        }
        if (value instanceof Collection) {
            return !((Collection<?>) value).isEmpty();
        }
        if (value instanceof Map) {
            return !((Map<?, ?>) value).isEmpty();
        }
        return true;
    }
    
    @Override
    public String getMessage(NotEmpty annotation) {
        return annotation.message();
    }
}